package com.Ngbussiness.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.Ngbussiness.dao.rechargeDao;
import com.Ngbussiness.util.Calenda;
import com.Ngbussiness.util.DatabaseAccess;
import com.Ngbussiness.util.EasyMapsManager;
import com.Ngbussiness.util.MD5;


public class rechargeDaoImpl implements rechargeDao{
	public JSONArray getplatformreve(int userid){
	JSONArray messagelist = new JSONArray();
	JSONObject message = new JSONObject();
	String sql = "select yj.price,yj.businessname,rm.add_time from red_money rm left join yongjin" +
			" yj on yj.Id=rm.yongjin_id where rm.member_id=?;";
	try {
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		EasyMapsManager easyMapsManager = new EasyMapsManager(
				databaseAccess);
		easyMapsManager.setPreparedParameter(userid);
		List list = easyMapsManager.executeQuery(sql);
		for(int i=0;i<list.size();i++){
			HashMap adu = (HashMap) list.get(i);
			double price=Double.parseDouble(adu.get("price").toString());
			String businessname=adu.get("businessname").toString();
			String add_time=adu.get("add_time").toString();
			message.put("time",add_time);
			message.put("source",businessname);
			message.put("money", price);
			message.put("category",0);
			messagelist.add(message);
			System.out.println("1"+message);
		}
		List list2 = new ArrayList();
		List list3 = new ArrayList();
				try {
					String sql2 = "select m.mobel,rc.charge_money,rc.charge_time from recharge rc left join " +
							"members m on m.Id=rc.member_id where rc.member_id=?;";
					DatabaseAccess databaseAccess1 = new DatabaseAccess(
							false);
					EasyMapsManager easyMapsManager1 = new EasyMapsManager(
							databaseAccess1);
					easyMapsManager1.setPreparedParameter(userid);
					list2 = easyMapsManager1.executeQuery(sql2);
					for(int i=0;i<list2.size();i++){
						HashMap adu = (HashMap) list2.get(i);
						double charge_money=Double.parseDouble(adu.get("charge_money").toString());
						String mobel=adu.get("mobel").toString();
						String charge_time=adu.get("charge_time").toString();
						message.put("time",charge_time);
						message.put("source",mobel);
						message.put("money", charge_money);
						message.put("category",1);
						messagelist.add(message);
					}
					try {
						String sql3 = "select uc.pay,uc.add_time,uc.member_id from `use-coupon` uc left join " +
								"yongjin yj on yj.Id=uc.yongjin_id where uc.is_use=1 and yj.member_id=?;";
						DatabaseAccess databaseAccess3 = new DatabaseAccess(
								false);
						EasyMapsManager easyMapsManager3 = new EasyMapsManager(
								databaseAccess3);
						easyMapsManager3.setPreparedParameter(userid);
						list3 = easyMapsManager3.executeQuery(sql3);
						for(int i=0;i<list3.size();i++){
							HashMap adu = (HashMap) list3.get(i);
							double pay=Double.parseDouble(adu.get("pay").toString());
							String member_id=adu.get("member_id").toString();
							String add_time=adu.get("add_time").toString();
							message.put("time",add_time);
							message.put("source",member_id);
							message.put("money", pay);
							message.put("category",3);
							messagelist.add(message);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
		databaseAccess.close();
		return messagelist;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
	
	public List checkexist(int userid, int yongjinid, String time1){
			DatabaseAccess dao=new DatabaseAccess();
			String sql="select * from `use-coupon`where member_id=? and yongjin_id=? and add_time like   '"+time1+"%'";
			try{
				DatabaseAccess databaseAccess = new DatabaseAccess(false);
				EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
				easyMapsManager.setPreparedParameter(userid);
				easyMapsManager.setPreparedParameter(yongjinid);
				List list = easyMapsManager.executeQuery(sql);
				databaseAccess.close();
				return list;
			}catch(Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	
	public List insertrc(int userid, int yongjinid, String time1){
		String sql = "select yj.probability,yj.price,yj.num,yj.member_id,count(rm.Id) as ucnum from yongjin yj left join `use-coupon` as" +
				" uc on uc.yongjin_id=yj.Id left join red_money rm on rm.yongjin_id=yj.Id where yj.Id=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(yongjinid);
			List list = easyMapsManager.executeQuery(sql);
			String sql2="update members set last_time=?,num=num+1 where Id=?";
			try{
				DatabaseAccess databaseAccess1 = new DatabaseAccess(false);
				String time2=time1.substring(0, 10);
				databaseAccess1.setPreparedParameter(time2);
				databaseAccess1.setPreparedParameter(userid);
				databaseAccess1.executeUpdate(sql2);
				databaseAccess1.close();
			}
			catch(Exception e) {
				e.printStackTrace();
			}
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void getuc(int userid, int yongjinid, String time1){
		String sql1="insert into `use-coupon`(yongjin_id,member_id,is_use,add_time) values(?,?,?,?); ";
		try{
			DatabaseAccess databaseAccess1 = new DatabaseAccess(false);
			databaseAccess1.setPreparedParameter(yongjinid);
			databaseAccess1.setPreparedParameter(userid);
			databaseAccess1.setPreparedParameter(0);
			databaseAccess1.setPreparedParameter(time1);
			databaseAccess1.executeInsert(sql1);
			databaseAccess1.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void getred(int userid, int yongjinid, String time1){
		String sql2="insert into red_money(yongjin_id,member_id,add_time) values(?,?,?); ";
		try{
			DatabaseAccess databaseAccess2 = new DatabaseAccess(false);
			databaseAccess2.setPreparedParameter(yongjinid);
			databaseAccess2.setPreparedParameter(userid);
			databaseAccess2.setPreparedParameter(time1);
			databaseAccess2.executeInsert(sql2);
			databaseAccess2.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void addmember(int member_id, double price){
		String sql2="update members set yongjin=yongjin+? where Id=?";
		try{
			DatabaseAccess databaseAccess2 = new DatabaseAccess(false);
			databaseAccess2.setPreparedParameter(price);
			databaseAccess2.setPreparedParameter(member_id);
			databaseAccess2.executeInsert(sql2);
			databaseAccess2.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
}
